<?php

if( isset( $_POST['options'] )) {
	foreach( $_POST['options'] as $cat => $options )
		foreach( $options as $key => $value )
			$conf->set( $cat, $key, $value );

	throw new redirect( MODUL_SELF );
}

$raw = iv::get('options');
$form = new form_renderer(MODUL_SELF);
$form->append($tabs = new widget_tabs());

foreach( $raw as $catid => $category) {
	$inputs = array();

	foreach( $category['items'] as $item ) {
		$name = "options[{$catid}][{$item['name']}]";
		$value = $conf->{$catid}->{$item['name']};

		switch( $item['type'] ) {
			case 'select':
				$inputs[] = new form_field_select( $name, $item['caption'], $item['options'], $value);
				break;
			case 'glob':
				$inputs[] = new form_field_select( $name, $item['caption'], globFiles( $item['pattern']), $value);
				break;
			case 'hidden':
				break;
			default:
				$inputs[] = new form_field_text( $name, $item['caption'], $value );
		}
	}

	$tabs->add(literal( $category['caption'] ), implode('', $inputs));
}

// not sure if 600px is nice especially for responsive design
$view->box( $form, 'Optionen bearbeiten', '600px' );